import router, { createRouter } from "@/router";
import staticRoutes from "@/router/routerMap/staticRoutes";
import dynamicRoutes from "@/router/routerMap/dynamicRoutes";
import storage from "@/utils/storage";
let routes = [...staticRoutes, ...dynamicRoutes]
export default {
    namespaced: true,
    state() {
        return {
            routes: [...staticRoutes, ...dynamicRoutes]
        }
    },
    getters: {},
    mutations: {
        // 更新routes
        filter_routes(state, payload) {
            state.routes = routes.filter(item => {
                if (!item.meta) {
                    return false
                } else {
                    return item.meta.roles.includes(payload)
                }
            })
        },


        updata_matcher() {
            //     1. 拿到dynamic动态路由
            //     2. 从本地获取role角色
            //     3. 根据role来筛选动态路由
            //     4. 创建一个新的router实例
            //     5. 将筛选的动态路由追加到新的router上
            //     6. 使用新的router.matcher去覆盖原本router.matcher
            let role = storage.roles;
            let newDynamicRoutes = dynamicRoutes.filter(item => {
                return item.meta.roles.includes(role);
            });
            let newRouter = createRouter(staticRoutes);
            // newRouter.addRoute()
            newDynamicRoutes.forEach(routeConfig => {
                newRouter.addRoute(routeConfig);
            });
            router.matcher = newRouter.matcher;
        }
    },
    actions: {},
    modules: {}
}